/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package spsutil;

import entities.Customer;
import entities.MaintenanceProject;
import entities.Vehicle;
import entities.WarrentyContract;
import entityFacades.CustomerFacadeLocal;
import entityFacades.MaintenanceProjectFacadeLocal;
import entityFacades.VehicleFacadeLocal;
import entityFacades.WarrentyContractFacadeLocal;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;

/**
 *
 * @author wesleycl
 */
public class TestDataGenerator {

    private EJBTestContext ejbtc  = null;

    public TestDataGenerator() {
        try {
            this.ejbtc = new EJBTestContext("localhost", "test", "test","mytestdb");
        } catch (NamingException ex) {
            System.out.println(ex.getMessage());
            Logger.getLogger(TestDataGenerator.class.getName()).
                    log(Level.SEVERE, null, ex);
        }
    }



    public static void main(String args [] ){
        
            TestDataGenerator t1 = new TestDataGenerator();
            
             //t1.genProjects(3000);
            //t1.genCustomer(3000);
            //t1.genVehicles(3000);
            t1.genWarrentyContractForEachProject(60);

    }



    public void genCustomer(int num_of_customer){
        try {
            CustomerFacadeLocal customerFacadeLocal = (CustomerFacadeLocal) this.ejbtc.getInitialContext()
                    .lookup("CustomerFacade/entityFacades.CustomerFacadeLocal");
            for (int i = 1; i <= num_of_customer; i++) {
                Customer c1 = new Customer();
                c1.setName("CustomerName" + i);
               
                customerFacadeLocal.create(c1);

            }
        } catch (NamingException ex) {
            System.out.println(ex.getMessage());
            Logger.getLogger(TestDataGenerator.class.getName()).
                    log(Level.SEVERE, null, ex);
        }


    }


    public void genVehicles(int num_of_vehicles){
        try {
            VehicleFacadeLocal vehicleFacadeLocal = (VehicleFacadeLocal) this.ejbtc.getInitialContext()
                    .lookup("VehicleFacade/entityFacades.VehicleFacadeLocal");
            CustomerFacadeLocal customerFacadeLocal = (CustomerFacadeLocal) this.ejbtc.getInitialContext()
                    .lookup("CustomerFacade/entityFacades.CustomerFacadeLocal");
            for (int i = 1; i <= num_of_vehicles; i++) {
                Vehicle v = new Vehicle();
                v.setCustomer(customerFacadeLocal.find(i));
                v.setVin("vin"+i);
                v.setVlicense("vlicence"+i);
                vehicleFacadeLocal.create(v);
            }
        } catch (NamingException ex) {
            System.out.println(ex.getMessage());
            Logger.getLogger(TestDataGenerator.class.getName()).
                    log(Level.SEVERE, null, ex);
        }

    }

    public void genProjects(int num_of_projects){
        try {
            VehicleFacadeLocal vehicleFacadeLocal = (VehicleFacadeLocal) this.ejbtc.getInitialContext()
                    .lookup("VehicleFacade/entityFacades.VehicleFacadeLocal");
            CustomerFacadeLocal customerFacadeLocal = (CustomerFacadeLocal) this.ejbtc.getInitialContext()
                    .lookup("CustomerFacade/entityFacades.CustomerFacadeLocal");
            MaintenanceProjectFacadeLocal maintenanceProjectFacadeLocal = (MaintenanceProjectFacadeLocal)this.ejbtc.getInitialContext()
                    .lookup("MaintenanceProjectFacade/entityFacades.MaintenanceProjectFacadeLocal");
            for (int i = 1; i <= num_of_projects; i++) {
                MaintenanceProject p = new MaintenanceProject();
                p.setCustomer(customerFacadeLocal.find(i));
                p.setProjectNumber("pn"+i);
                p.setVehicle(vehicleFacadeLocal.find(i));
                maintenanceProjectFacadeLocal.create(p);
            }
        } catch (NamingException ex) {
            System.out.println(ex.getMessage());
            Logger.getLogger(TestDataGenerator.class.getName()).
                    log(Level.SEVERE, null, ex);
        }


    }



        public void genWarrentyContractForEachProject(int num_of_contracts){
        try {
            VehicleFacadeLocal vehicleFacadeLocal = (VehicleFacadeLocal) this.ejbtc.getInitialContext()
                    .lookup("VehicleFacade/entityFacades.VehicleFacadeLocal");
            CustomerFacadeLocal customerFacadeLocal = (CustomerFacadeLocal) this.ejbtc.getInitialContext()
                    .lookup("CustomerFacade/entityFacades.CustomerFacadeLocal");
            MaintenanceProjectFacadeLocal maintenanceProjectFacadeLocal = (MaintenanceProjectFacadeLocal)this.ejbtc.getInitialContext()
                    .lookup("MaintenanceProjectFacade/entityFacades.MaintenanceProjectFacadeLocal");
            WarrentyContractFacadeLocal warrentyContractFacadeLocal = (WarrentyContractFacadeLocal)
                    this.ejbtc.getInitialContext().lookup("WarrentyContractFacade/entityFacades.WarrentyContractFacadeLocal");
            
            
            for (int i = 1; i <= num_of_contracts; i++) {
               WarrentyContract w =  new WarrentyContract();
               w.setContractStartDate(new Date());
               w.setContractEndDate(new Date());
               //w.setId(i);
               w.setVehicle(vehicleFacadeLocal.find(i));

               warrentyContractFacadeLocal.create(w);


               MaintenanceProject mp = maintenanceProjectFacadeLocal.find(i);
               mp.setWarrentyContract(w);
               maintenanceProjectFacadeLocal.edit(mp);

            }
        } catch (NamingException ex) {
            System.out.println(ex.getMessage());
            Logger.getLogger(TestDataGenerator.class.getName()).
                    log(Level.SEVERE, null, ex);
        }


    }

    /**
     * @return the ejbtc
     */
    public EJBTestContext getEjbtc() {
        return ejbtc;
    }

    /**
     * @param ejbtc the ejbtc to set
     */
    public void setEjbtc(EJBTestContext ejbtc) {
        this.ejbtc = ejbtc;
    }

}
